package org.sn.jdish.algorithm.sort;

/**
 * 排序测试
 * 
 * @author snzigod@hotmail.com
 *
 */
public class SortTest {

	public static void main(String[] args) {
		int maxValue = 99999;
		int[] array = getRandomArray(30, maxValue);
		consoleOutArray(array);
		System.out.println("==========冒泡排序==========");
		array = getRandomArray(30, maxValue);
		consoleOutArray(SortUtil.bubbleSort(array));
		System.out.println("==========选择排序==========");
		array = getRandomArray(30, maxValue);
		consoleOutArray(SortUtil.selectionSort(array));
		System.out.println("==========插入排序==========");
		array = getRandomArray(30, maxValue);
		consoleOutArray(SortUtil.insertSort(array));
		System.out.println("==========希尔排序==========");
		array = getRandomArray(30, maxValue);
		consoleOutArray(SortUtil.shellSort(array));
		System.out.println("==========归并排序==========");
		array = getRandomArray(30, maxValue);
		consoleOutArray(SortUtil.mergeSort(array));
		System.out.println("==========快速排序==========");
		array = getRandomArray(30, maxValue);
		consoleOutArray(SortUtil.quickSort(array));
		System.out.println("==========堆排序==========");
		array = getRandomArray(30, maxValue);
		consoleOutArray(SortUtil.heapSort(array));
		System.out.println("==========计数排序==========");
		array = getRandomArray(30, maxValue);
		consoleOutArray(SortUtil.countingSort(array, maxValue));
		System.out.println("==========桶排序==========");
		array = getRandomArray(30, maxValue);
		consoleOutArray(SortUtil.bucketSort(array));
		System.out.println("==========基数排序==========");
		array = getRandomArray(30, maxValue);
		consoleOutArray(SortUtil.radixSort(array, 10, String.valueOf(maxValue).length()));
	}

	private final static String splitStr = "|";

	/**
	 * 产生随机数组
	 * 
	 * @param arrayLength
	 *            数组长度
	 * @param maxArrayValue
	 *            最大数值
	 * @return 随机数组
	 */
	private static int[] getRandomArray(int arrayLength, int maxArrayValue) {
		int[] array = new int[arrayLength];
		for (int i = 0; i < array.length; i++) {
			array[i] = (int) (Math.random() * maxArrayValue);
		}
		return array;
	}

	/**
	 * 控制台输出数组
	 * 
	 * @param array
	 *            数组
	 */
	private static void consoleOutArray(int[] array) {
		for (int i : array) {
			System.out.print(i + splitStr);
		}
		System.out.println();
	}

}
